package com.study.leetcode;

//https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/
public class RemoveNthNodeFromEndOfList {
    private int count = 0;

    /**
     * 这里使用递归来经行反向计数也就是从尾部开始计数
     */
    public ListNode removeNthFromEnd(ListNode head, int n) {
        //遍历到末尾返回null
        if (head == null) return null;
        //这里接收返回来的链表
        head.next = removeNthFromEnd(head.next, n);
        count++;
        //如果反向计数等于要移除的则直接跳过这个元素
        if (count == n) return head.next;
        //普通情况直接返回
        return head;
    }
}
